package com.javandad.app.client.searchComponent;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.javandad.app.shared.entity.project.Project;
import com.javandad.app.shared.entity.search.AdvancedProjectSearchProperties;
import com.javandad.app.shared.entity.search.SearchProperties;

class ProjectSearch extends Search {
	
	private FlexTable opts = new FlexTable();
	private ListBox projectType = new ListBox(false);
	
	public ProjectSearch(TextBox freetext) {
		super(freetext);
		initAdvancedProjectSearch();
	}

	private void initAdvancedProjectSearch() {
		opts.setText(0, 0, "Project Type:");
		projectType.addItem("All", ((Integer)0).toString());
		projectType.addItem("Application", ((Integer)Project.TYPE_APPLICATION).toString());
		projectType.addItem("Library", ((Integer)Project.TYPE_LIBRARY).toString());
		opts.setWidget(0, 1, projectType);
	}
	
	@Override
	public Widget getAdvancedOptions() {
		return opts;
	}

	@Override
	public SearchProperties getSearchProperties() throws InvalidArgumentException {
		// freetext:
		String freetext = freetextSearchTerm.getText();
		
		// advanced project search options:
		int type = Integer.parseInt(projectType.getValue(projectType.getSelectedIndex()));
		
		AdvancedProjectSearchProperties apsp = new AdvancedProjectSearchProperties(freetext, type);
		
//		if(freetext.equals("") && !apsp.anythingSet())
//			throw new InvalidArgumentException("You have to type something to search for...");
		
		return apsp;
	}

	@Override
	public String getSearchName() {
		return "Projects";
	}
}